package com.chenjt.three_tools;

import com.google.common.collect.Lists;
import dev.langchain4j.agent.tool.*;
import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.ChatMessage;
import dev.langchain4j.data.message.ToolExecutionResultMessage;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.model.output.Response;
import dev.langchain4j.service.tool.DefaultToolExecutor;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

/**
 * 注释：
 *
 * @author chenjt
 * @date 2025/3/7 16:25
 */
public class ToolsDemo2 {

    public static void main(String[] args) throws Exception {


        ChatLanguageModel model = OpenAiChatModel.builder()
                .apiKey("demo")
                .modelName("gpt-4o-mini")
                .build();


        // 构建工具
        List<ToolSpecification> toolSpecifications = ToolSpecifications.toolSpecificationsFrom(WeatherUtil.class);

        // 构建对话消息
        List<ChatMessage> chatMessages = new ArrayList<>();

        UserMessage userMessage = UserMessage.from("广州市今天的天气怎么样？");
        chatMessages.add(userMessage);

        // 第一次与AI大模型交互，获得需要调用的工具
        Response<AiMessage> aiMessageResponse = model.generate(chatMessages, toolSpecifications);

        AiMessage aiMessage = aiMessageResponse.content();
        // AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = "call_b7nt8fvQUvTK2RnzQ36ot0KS", name = "dataUtil", arguments = "{}" }] }
        System.out.println(aiMessage);

        List<ToolExecutionRequest> toolExecutionRequests = aiMessage.toolExecutionRequests();

        // 本地执行工具的方法
        if(aiMessage.hasToolExecutionRequests()){
            for(ToolExecutionRequest toolExecutionRequest : toolExecutionRequests){
                String methodName = toolExecutionRequest.name();
                String arguments = toolExecutionRequest.arguments();

                System.out.println("调用的方法" + methodName);
                System.out.println("调用的参数" + arguments);

            }

        }
        chatMessages.add(aiMessage);

        WeatherUtil weatherUtil = new WeatherUtil();

        // 将工具调用的方法与聊天消息一起传给AI大模型
        toolExecutionRequests.forEach(toolExecutionRequest -> {

            DefaultToolExecutor toolExecutor = new DefaultToolExecutor(weatherUtil, toolExecutionRequest);
            String result = toolExecutor.execute(toolExecutionRequest, UUID.randomUUID().toString());
            System.out.println("工具执行结果：" + result);
            ToolExecutionResultMessage toolExecutionResultMessage = ToolExecutionResultMessage.from(toolExecutionRequest, result);
            chatMessages.add(toolExecutionResultMessage);

        });

        // 拿到本地工具执行的最终结果
        AiMessage finalResponse = model.generate(chatMessages).content();
        System.out.println("最终结果：" + finalResponse.text());


    }



    static class WeatherUtil{
        @Tool("获取某一个具体城市的天气")
        public String getWeather(@P("指定的城市")String city){
            return city + "今天是晴天";
        }
    }



}
